package org.etom.impl.ui.views;

import java.io.IOException;

import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.WorkbenchException;
import org.etom.connector.datatypes.IMoodleConnection;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.impl.ui.actions.DeleteAccountAction;
import org.etom.impl.ui.actions.DownloadAsAction;
import org.etom.impl.ui.actions.EditAccountAction;
import org.etom.impl.ui.actions.NewAccountAction;
import org.etom.impl.ui.actions.RefreshAction;
import org.etom.impl.ui.actions.SaveAsAction;
import org.etom.impl.ui.common.ResourcesTreeViewer;
import org.etom.impl.ui.util.Logger;

/**
 * The class extends <code>AbstractResourceView</code>
 * with additional functionality for the event handling.
 */
public final class ResourcesView
                    extends AbstractResourcesView
                    implements IMenuListener {

  private ResourcesTreeViewer resourcesTreeViewer;
  
  /*===[AbstractResourcesView methods][Start]===*/
  @Override
  public void init(IViewSite site, IMemento memento) throws PartInitException {
    super.init(site, memento);
  }

  /**
   * @see org.eclipse.ui.part.WorkbenchPart#dispose()
   */
  @Override
  public void dispose() {
    saveState();
    super.dispose();
  }

  /*
   * The method saves the state of the recourse viewer.
   */
  private void saveState() {
    if (resourcesTreeViewer != null) {
      try {
        resourcesTreeViewer.save();
      } catch (IOException ioe) {
        Logger.ERROR(
            ViewsMessages.Connections_Save_Fail, ioe);
      }
    }
  }
  
  /**
   * @see org.etom.impl.ui.views.AbstractResourcesView#createPartControl(org.eclipse.swt.widgets.Composite)
   */
  @Override
  public void createPartControl(Composite parent) {
    super.createPartControl(parent);
    menuManager.addMenuListener(this);
    initTreeViewer();
  }
  /*===[AbstractResourcesView methods][End]===*/
  
  /*===[IMenuListener methods][Start]===*/
  @Override
  public void menuAboutToShow(IMenuManager manager) {
    fillContextMenu(manager);
  }
  /*===[IMenuListener methods][End]===*/
  
  /*
   * The method checks the current selection on the viewer
   * and fills the context menu.
   */
  private void fillContextMenu(IMenuManager manager) {
    manager.add(new NewAccountAction(parent.getShell(), resourcesTreeViewer));
    Object selection = resourcesTreeViewer.getSelection();
    if (selection instanceof IMoodleConnection) {
      manager.add(new EditAccountAction(parent.getShell(), resourcesTreeViewer));
      manager.add(new DeleteAccountAction(parent.getShell(), resourcesTreeViewer));
    } else if (selection instanceof IMoodleModule) {
      IMoodleModule moodleModule = (IMoodleModule) selection;
      manager.add(new DownloadAsAction(parent.getShell(), moodleModule));
      manager.add(new SaveAsAction(parent.getShell(), moodleModule));
    }
    manager.add(new RefreshAction(resourcesTreeViewer));
  }
  
  /*
   * Creates the tree viewer.
   */
  private void initTreeViewer() {
    if (resourcesTreeViewer != null) return;
    resourcesTreeViewer = new ResourcesTreeViewer(
        treeViewer, IMoodleModule.RESOURCE);
    try {
      resourcesTreeViewer.load();
    } catch (WorkbenchException we) {
      Logger.ERROR(
          ViewsMessages.Connections_Load_Fail, we);
    }
  }
  
}
